/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package maindatart.view;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import maindatart.metadata.MDMetaDataService;
import maindatart.metadata.MainDataMetaDataEntity;
import maindatart.metadata.MainDataPropertyEntity;
import maindatart.rush.alg.DataSameAlgFactory;
import maindatart.rush.config.MainDataRashConfig;
import maindatart.rush.config.PropertityConfig;
import maindatart.rush.config.RushConfigService;

/**
 *
 * @author Administrator
 */
public class AlgDesignPanel extends javax.swing.JPanel {

    private List<MainDataMetaDataEntity> mdmdList = null;
    private MDMetaDataService service = new MDMetaDataService();
    private RushConfigService rcService = new RushConfigService();
    /**
     * Creates new form AlgDesignPanel
     */
    public AlgDesignPanel() {
        initComponents();
        formateTable();
        initData();
    }
    private void formateTable(){
        JComboBox comboBox = new JComboBox();  
        for(String name : DataSameAlgFactory.typeList){
            comboBox.addItem(name);  
        }
        
        //TableColumn主要作用可以对表格中的某一列的属性进行编辑  
        TableColumn tableColumn = jTable1.getColumn("algname");  
          
        //利用TableColumn类中的setCellEditor()方法来设置单元格的编辑器  
        //DefaultCellEditor类可以将表格中的单元格设置成下拉框  
        tableColumn.setCellEditor(new DefaultCellEditor(comboBox));  
    }
     private void initData(){
        setJpanel1Data();
    }
    private void setJpanel1Data(){
        
        DefaultListModel dlm = new DefaultListModel();
        try {
            mdmdList = service.getAllRegMainData();
            for(MainDataMetaDataEntity mdmd : mdmdList){
            String name = mdmd.getMainDataName();
            dlm.addElement(name);
        }
        jList1.setModel(dlm);
        jList1.repaint();
        } catch (Exception ex) {
            Logger.getLogger(MainDataRegPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jButton2 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                showRushAlg(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "propertityname", "propration", "algname"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable1);

        jLabel2.setText(">相同>");

        jLabel3.setText(">相似>");

        jLabel4.setText(">不同>");

        jButton1.setText("save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel5.setText("主数据列表");

        jList2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                showAlgDetail(evt);
            }
        });
        jScrollPane3.setViewportView(jList2);

        jButton2.setText("new");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel7.setText("100%");
        jLabel7.setToolTipText("");

        jLabel8.setText("0%");

        jLabel9.setText("清洗算法列表");
        jLabel9.setToolTipText("");

        jLabel10.setText("设定不同的相似度以便于计算机自动处理清洗的结果：");

        jLabel11.setText("为每个属性设定比较算法和权值");

        jLabel1.setText("算法编号：");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel9)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel2)
                                .addGap(35, 35, 35)
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel3)
                                .addGap(33, 33, 33)
                                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel8))
                            .addComponent(jLabel10)
                            .addComponent(jLabel11)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 573, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton2)
                                .addGap(109, 109, 109)
                                .addComponent(jButton1)
                                .addGap(196, 196, 196))))
                    .addComponent(jLabel5))
                .addContainerGap(80, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel9)
                        .addGap(13, 13, 13)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(17, 17, 17)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel11)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addGap(20, 20, 20))
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * 新增一个算法
     * @param evt 
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        String currentName = (String)jList1.getSelectedValue();
        if(currentName == null){
            JOptionPane.showMessageDialog(this.getParent(),"请指定一个主数据");
            return;
        }
            
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField1.setText("");

        DefaultTableModel tm = (DefaultTableModel)jTable1.getModel();
        tm.setRowCount(0);
        
        for(MainDataMetaDataEntity mdmd : mdmdList){
            if(currentName.equals(mdmd.getMainDataName())){
                List<MainDataPropertyEntity> ps = mdmd.getProperties();
                for(MainDataPropertyEntity p : ps){
                    Vector v = new Vector();
                    v.add(p.getName());
                    tm.addRow(v);
                }
            }
        }
        jTable1.setModel(tm);
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * 显示清洗算法
     * @param evt 
     */
    private void showRushAlg(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_showRushAlg
        String currentName = (String)jList1.getSelectedValue();
        try {
            List<MainDataRashConfig> configs = rcService.getMainDataRashConfigs(currentName);
            DefaultListModel dlm = new DefaultListModel();
            
            for(MainDataRashConfig c : configs){
                String id = c.getId();
                dlm.addElement(id);
            }
            jList2.setModel(dlm);
            jList2.repaint();
        } catch (Exception ex) {
            Logger.getLogger(AlgDesignPanel.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this.getParent(), "出问题了" + ex.getMessage());
        }
        
    }//GEN-LAST:event_showRushAlg

    /**
     * 显示算法明细信息
     * @param evt 
     */
    private void showAlgDetail(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_showAlgDetail
        // TODO add your handling code here:
        String id = (String)jList2.getSelectedValue();
        try {
            MainDataRashConfig mdrc = rcService.getMainDataRashConfig(id);
            
            double like = mdrc.getSame();
            double different = mdrc.getDifferent();
            
            jTextField2.setText(Double.toString(like));
            jTextField3.setText(Double.toString(different));
            jTextField1.setText(id);
            
            List<PropertityConfig> pcs = mdrc.getItemConfig();
            DefaultTableModel tm = (DefaultTableModel)jTable1.getModel();
            tm.setRowCount(0);
            for(PropertityConfig pc : pcs){
                String name = pc.getItmename();
                String alg = pc.getAlgname();
                double proportion = pc.getProportion();
                Vector v = new Vector();
                v.add(name);
                v.add(proportion);
                v.add(alg);
                
                tm.addRow(v);
            }
            jTable1.setModel(tm);
        } catch (Exception ex) {
            Logger.getLogger(AlgDesignPanel.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this.getParent(), "出事了！" + ex.getMessage());
        }
    }//GEN-LAST:event_showAlgDetail

    /**
     * 增加一个清洗算法
     * @param evt 
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        MainDataRashConfig config = getMainDataRashConfig();
        try {
            rcService.addMainDataRashConfig(config);
            JOptionPane.showMessageDialog(this.getParent(), "保存成功！");
        } catch (Exception ex) {
            Logger.getLogger(AlgDesignPanel.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this.getParent(), "没有插入进去啊！" + ex.getMessage());
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private MainDataRashConfig getMainDataRashConfig(){
        MainDataRashConfig mdrc = new MainDataRashConfig();
        mdrc.setId(jTextField1.getText());
        mdrc.setMainDataName((String)jList1.getSelectedValue());
        mdrc.setSame(new Double(jTextField2.getText()));
        mdrc.setDifferent(new Double(jTextField3.getText()));
        
        List<PropertityConfig> pcs = new ArrayList();
        DefaultTableModel tm = (DefaultTableModel)jTable1.getModel();
        Vector<Vector> vs = tm.getDataVector();
        for(Vector v : vs){
            PropertityConfig pc = new PropertityConfig();
            String name = (String)v.get(0);
            double propration = new Double((String)v.get(1));
            String alg = (String)v.get(2);
            pc.setItmename(name);
            pc.setProportion(propration);
            pc.setAlgname(alg);
           pcs.add(pc);
        }
        
        mdrc.setItemConfig(pcs);
        return mdrc;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}
